/**
 *@name Ccay.hypertext.EditHyperText
 *@description HyperText 视图页面控制
 *@param page 
 */
Ccay.Module.define("Ccay.hypertext.EditHyperText",function(page,$S){
	/**
	 * 导入枚举类型
	 */
	Enum.import("HyperCategoryEnum");
	
	var isCreate = true;
	var items = [];
	var editorBox;
	var isResourceEdit = false;
	page.init = function(category,id) {
		initHtml(category,id);
	};
	
	/**
	 * 根据设置的数据字典的开关，来是否启动编辑模式，默认关闭
	 */
	initHtml = function(category,id){
		   items=['source','|','undo', 'redo', '|', 'preview', 'print', 'template', 'cut', 'copy', 'paste',
				'plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright',
				'justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript',
				'superscript', 'clearhtml', 'quickformat', 'selectall',
				'formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold',
				'italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'table', 'hr',
				'emoticons', 'code', 'pagebreak', 'anchor', 'link', 'unlink', '|', 'about','fullscreen'];
			var options = {
				width : '563px',
				height : '300px',
				items : items,
				uploadJson : 'servlet/upload?ulType=hyperTextImage',
				allowFileManager : true
			};
		    //KindEditor的option{resizeType}  为2时可以拖动改变宽度和高度，为1时只能改变高度，0时不能拖动
		    editorBox = KindEditor.create('#editor',options);
		    //判断添加和更新模式来进行数据初始化
		    if(category && id){
				isCreate = false;
				Ccay.Common.formFill("#editHyperTextForm","services/manage/hypertext/find?category={0}&id={1}".formatURI(category,id),
						function(data){
							//富文本组件内容填充
						    editorBox.html((data.content+"").encodeHtml());
							var type = $S("#editHyperTextForm").nameChild("category").ccayGetValue();
					  });
			};
			
	};
	
	/**
	 * onchange事件，下拉框为MailTemplate或Resources时，显示textarea
	 */
	page.onTypeChange=function(input,value,text, oldValue,item,selector,data,noConnect){
		for(attr in Enum.HyperCategoryEnum){
			if(Enum.HyperCategoryEnum[attr] == input){
				$S("."+attr).show();
			}else{
				$S("."+attr).hide();
			}
		}
	}
	
	/**
	 * @description 数据保存方法 create&update
	 */
	page.save=function(){
		var form = $S("#editHyperTextForm");
		var datas=form.jsonData(); 
		datas.content = editorBox.html();
		if(editorBox.isEmpty()){
			Ccay.UI.showMessage("ccay.hypertext.label.context.required","warn");
			return;
		}
	
		if(form.valid()){			
			Ccay.Common.save(true,"services/manage/hypertext/create",datas,function(){ 
				Ccay.UI.topBox(isCreate ? "ccay.common.message.successAdd" : "ccay.common.message.successUpdate");
				page.close(true);
			});	
		}
	};
	
	/**
	 * 关闭对话
	 */
	page.close = function(isReload)
	{		
		$('#listHyperTextTab').ccayTabRemoveItem();
		if(isReload){
		  $("#hyperTextGrid").manager().reload(true,true);
		}
	}

});

